//
//  ZGLweather.m
//  Faker
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLweather.h"
#import "ZGLweatherModel.h"
#import "ZGLHTTPTOOL.h"
@interface ZGLweather ()
@property (strong, nonatomic) IBOutlet UILabel *rt_temperature;
@property (strong, nonatomic) IBOutlet UILabel *temperature;
@property (strong, nonatomic) IBOutlet UILabel *week;
@property (strong, nonatomic) IBOutlet UILabel *pm2_5;
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UILabel *climate;
@property (strong, nonatomic) IBOutlet UILabel *city;
@property (strong, nonatomic) ZGLweatherModel *mod;
@property (copy, nonatomic) NSString *addr;
@end
@implementation ZGLweather
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:@"ZGLweather" owner:self options:nil];;
    }
    return self;
}

- (void)getAdd{
    __weak typeof(self) weakSelf = self;
    _mod = [[ZGLweatherModel alloc]init];
    [self asynGETJSONWithURL:@"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" completion:^(id result) {
        weakSelf.addr = [NSString stringWithFormat:@"%@|%@", result[@"province"], result[@"city"]];
        [weakSelf handleData];
    }];
}

- (void)handleData{
    [ZGLHTTPTOOL GETWithURL:[[NSString stringWithFormat:@"http://c.m.163.com/nc/weather/%@.html", self.addr]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:result];
        [self.mod setValuesForKeysWithDictionary:dic];//dt,rt_temperature
        [self.mod setValuesForKeysWithDictionary:[dic objectForKey:@"pm2d5"]];//pm2_5
        NSArray *city = [dic objectForKey:self.addr];
        [self.mod setValuesForKeysWithDictionary:[city firstObject]];//当日天气
        [self setValues];
    }withFail:^(NSError *error) {
        NSLog(@"没网:ZGLweather");
    }];
}

- (void)setValues{
    _rt_temperature.text = [NSString stringWithFormat:@"%ld", _mod.rt_temperature];
    _temperature.text = _mod.temperature;
    _week.text = [NSString stringWithFormat:@"%@ %@", _mod.dt, _mod.week];
    NSString *str = [[NSString alloc]init];
    NSLog(@"%ld", _mod.pm2_5);
    if (_mod.pm2_5 <= 35) {
        str = @"优";
    }else if (_mod.pm2_5 <= 75){
        str = @"良";
    }else if (_mod.pm2_5 <= 115){
        str = @"轻度污染";
    }else if (_mod.pm2_5 <= 150){
        str = @"中度污染";
    }else if (_mod.pm2_5 <= 250){
        str = @"重度污染";
    }else{
        str = @"严重污染";
    }
    _pm2_5.text = [NSString stringWithFormat:@"PM2.5 %lu %@", _mod.pm2_5, str];
    _climate.text = [NSString stringWithFormat:@"%@ %@", _mod.climate, _mod.wind];
    _city.text = [_addr stringByReplacingOccurrencesOfString:@"|" withString:@" "];
}

- (void)asynGETJSONWithURL:(NSString *)urlString completion:(void(^)(id result))block {
    /* 1.转码 */
    NSString *enCodingStr = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    /* 2.创建URL对象 */
    NSURL *url = [NSURL URLWithString:enCodingStr];
    /* 3.创建请求 */
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    /* 4.创建链接接受数据 */
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      /* 5.数据解析 */
                                      NSString *temResult = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
                                      if (temResult.length == 0) {
                                          return ;
                                      }
                                      temResult = [temResult substringFromIndex:21];
                                      temResult = [temResult substringToIndex:temResult.length - 1];
                                      NSData* xmlData = [temResult dataUsingEncoding:NSUTF8StringEncoding];
                                      id result = [NSJSONSerialization JSONObjectWithData:xmlData options:NSJSONReadingMutableContainers error:&error];
                                      /* 6.通过block将result值返回出去 */
                                      block(result);
                                  }];
    [task resume];
}
@end